
#include "system_config.h"

// PIC32 configuration bits
#if defined(__32MX270F256B__)
    // Oscillator
    #pragma config FNOSC    = PRIPLL        // Oscillator Selection
    #pragma config POSCMOD  = EC            // Primary Oscillator
    #pragma config OSCIOFNC = OFF           // CLKO Enable
    // 8 Mhz
    #pragma config UPLLIDIV = DIV_2         // USB PLL Input Divider
    #pragma config FPLLIDIV = DIV_2         // PLL Input Divider
    // Other
    #pragma config UPLLEN   = ON            // USB PLL Enabled
    #pragma config FPLLMUL  = MUL_20        // PLL Multiplier
    #pragma config FPLLODIV = DIV_2         // PLL Output Divider
    #pragma config FPBDIV   = DIV_1         // Peripheral Clock divisor
    #pragma config FWDTEN   = OFF           // Watchdog Timer
    #pragma config WDTPS    = PS1           // Watchdog Timer Postscale
    #pragma config FCKSM    = CSDCMD        // Clock Switching & Fail Safe Clock Monitor
    #pragma config IESO     = OFF           // Internal/External Switch-over
    #pragma config FSOSCEN  = OFF           // Secondary Oscillator Enable
    #pragma config CP       = OFF           // Code Protect
    #pragma config BWP      = OFF           // Boot Flash Write Protect
    #pragma config PWP      = OFF           // Program Flash Write Protect
    #pragma config ICESEL   = ICS_PGx1      // ICE/ICD Comm Channel Select
    #pragma config DEBUG    = OFF           // Debugger Disabled for Starter Kit
    #pragma config JTAGEN   = OFF           // No JTAG.  Need this for RB9 to work!
/*
    #define FLASH_PAGE_SIZE             1024
    #define BOOTLOADER_SIZE             0x0BEF
    #define BOOTLOADER_BASE_ADDRESS     0x9FC00000
    #define FLASH_BASE_ADDRESS          0x9D000000
    #define FLASH_END_ADDRESS           (0x9D000000+BMXPFMSZ-1)
    #define USER_APP_RESET_ADDRESS      (0x9D006000 + 0x1000)
    #define FLASH_PAGES                 (FLASH_END_ADDRESS - FLASH_BASE_ADDRESS + 1)/FLASH_PAGE_SIZE
*/
#else
    #error "Invalid part"
#endif

#include <stdio.h>
#include <stdlib.h>

#ifdef HARMONY
#include "system/devcon/sys_devcon.h"
#include "system/ports/sys_ports.h"
#include "system/int/sys_int.h" // Harmony
#else
#include <plib.h>
#endif

#include "PS2Protocol.h"
#include "RingBuffer.h"
#include "PS2Keyboard.h"

#ifdef PS2_KEYBOARD_TEST
unsigned char keyboardData[100][3];
unsigned char dataIndex = 0;

void PS2_Keyboard_Int(unsigned char code1, unsigned char code2, unsigned char code3){
    keyboardData[dataIndex][0] = code1;
    keyboardData[dataIndex][1] = code2;
    keyboardData[dataIndex++][2] = code3;
    if (dataIndex == 15){
        int a = 0;
    }
}
#else

#ifdef PS2_PROTOCOL_INT
void PS2_Available_Int(){
    unsigned char code;
    code = PS2_GetScanCode();   // fetch the scan code
    AddToBuffer(code);
    if (code == 0x1c){
        int a = 1;
        a++;
        PS2_Available();
    } 
}
#endif
#endif

main()
{
    unsigned char code;
    int gotkey = 0;

#ifdef HARMONY
    // Should init clock but we are not real Harmony app.
    // SYS_Initialize ( NULL );
#else
    SYSTEMConfig( SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
#endif

#ifdef PS2_KEYBOARD_TEST
    PS2K_Init();
#else
    PS2_Init();                  // kbd initialization
#endif

#ifdef HARMONY
    PLIB_INT_MultiVectorSelect(INT_ID_0);
    PLIB_INT_Enable(INT_ID_0);
#else
    INTEnableSystemMultiVectoredInt();
#endif

#ifndef PS2_KEYBOARD_TEST
#ifdef PS2_PROTOCOL_INT
    PS2_Callback(PS2_Available_Int);
#endif

    PS2_Send(0xff); // Reset keyboard.  Keyboard ill return 0xFA as result if success.

#else
    memset(keyboardData,0,sizeof(keyboardData));
    
    PS2K_Callback(PS2_Keyboard_Int);
    PS2K_ResetKeyboard();
    PS2K_Echo();
    PS2K_SetLED(PS2K_NUM_LOCK | PS2K_CAPS_LOCK);
    PS2K_SetToDefaults();
    PS2K_DisableScanning();
    PS2K_EnableScanning();
    PS2K_Echo();
    PS2K_ResendLastByte();
    PS2K_SetScanCodeSet(PS2K_SET_2);
    PS2K_SetTypematicRateDelay(0,PS2K_DELAY_1000ms);
    PS2K_ResetKeyboard();

#endif



    while (1){
        while ( !gotkey)
        {

#ifndef PS2_PROTOCOL_INT

            if ( PS2_Available() )        // wait for the flag
            {
                code = PS2_GetScanCode();   // fetch the scan code
                AddToBuffer(code);
                if (code == 0x1c){
                    int a = 1;
                    a++;
                    PS2_Available();
                }
            }
#endif
        } 
    }
} 